<template>
  <div class="container">
    <div class="app-container">
      <div class="role-container">
        <div class="left-wrapper">
          <div
            v-for="(item,index) in roleList"
            :key="item.roleId"
            class="role-item"
            :class="{active: index === activeIndex}"
            @click="changeRole(index)"
          >
            <div class="role-info">
              <svg-icon icon-class="user" class="icon" />
              {{ item.roleName }}
            </div>
            <div class="more">
              <svg-icon icon-class="more" />
            </div>
          </div>
          <el-button class="addBtn" size="mini">添加角色</el-button>
        </div>
        <!-- 右侧权限和成员 -->
        <div class="right-wrapper">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <div class="tree-title"> {{ item.title }}</div>
              <el-tree
                :data="item.children"
                node-key="id"
                :props="{ label: 'title' }"
                show-checkbox
                check-strictly
                :default-expand-all="true"
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getRoleListApi, getTreeListApi } from '@/api/system'

export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      activeIndex: 0,
      treeList: []
    }
  },
  mounted() {
    this.getRoleList()
    this.getTreeList()
  },
  methods: {
    async getRoleList() {
      const res = await getRoleListApi()
      this.roleList = res
    },
    // 切换角色
    changeRole(idx) {
      this.activeIndex = idx
    },
    // 递归处理函数
    addDisabled(treeList) {
      treeList.forEach(item => {
        item.disabled = true
        // 递归处理
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    async getTreeList() {
      const res = await getTreeListApi()
      this.treeList = res
      // 禁用
      // 目标：treeList里面的每一项以及嵌套的子项都添加一个disabled = true
      this.addDisabled(this.treeList)
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
